package com.atguigu.gmall.item.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class ItemServiceImpl implements ItemService {

    @Resource
    private ProductFeignClient productFeignClient;

    @Resource
    private ThreadPoolExecutor threadPoolExecutor;

    @Resource
    private ListFeignClient listFeignClient;

    //查询五部分数据
    @Override
    public Map<String, Object> getItem(Long skuId) {
        HashMap<String, Object> hashMap = new HashMap<>();

        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //查询skuinfo基本信息及图片信息
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            hashMap.put("skuInfo", skuInfo);
            return skuInfo;
        }, threadPoolExecutor);

        CompletableFuture<Void> priceCompletableFuture = CompletableFuture.runAsync(() -> {
            //查询Sku价格信息
            BigDecimal price = productFeignClient.getPrice(skuId);
            hashMap.put("price", price);
        }, threadPoolExecutor);

        CompletableFuture<Void> baseCategoryViewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync((skuInfo) -> {
            //查询一级、二级、三级分类信息
            BaseCategoryView baseCategoryView = productFeignClient.getBaseCategoryView(skuInfo.getCategory3Id());
            hashMap.put("categoryView", baseCategoryView);
        }, threadPoolExecutor);

        CompletableFuture<Void> spuSaleAttrListCheckBySkuCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync((skuInfo) -> {
            //查询Sku总销售属性及属性值 同时默认选中一个商品
            List<SpuSaleAttr> spuSaleAttrListCheckBySku = productFeignClient
                    .getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
            hashMap.put("spuSaleAttrList", spuSaleAttrListCheckBySku);
        }, threadPoolExecutor);

        CompletableFuture<Void> skuValueIdsMapCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync((skuInfo) -> {
            //查询页面上组合与skuId的关系
            Map<String, String> skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            hashMap.put("valuesSkuJson", JSONObject.toJSONString(skuValueIdsMap));
        }, threadPoolExecutor);

        //增加热度
        CompletableFuture.runAsync(() -> {
            listFeignClient.hotScore(skuId);
        },threadPoolExecutor);

        CompletableFuture.allOf(
                skuInfoCompletableFuture,
                priceCompletableFuture,
                baseCategoryViewCompletableFuture,
                spuSaleAttrListCheckBySkuCompletableFuture,
                skuValueIdsMapCompletableFuture).join();
        return hashMap;
    }
}
